
import 'package:date_format/date_format.dart';
import 'package:ycbl/common/my_course_type.dart';
import 'package:ycbl/config/bl_constant.dart';
import 'package:ycbl/main.dart';
import 'package:ycbl/model/bl_error_entity.dart';
import 'package:ycbl/model/calendar_task_list_entity.dart';
import 'package:ycbl/model/free_daily_list_entity.dart';
import 'package:ycbl/model/learn_course_detail_entity.dart';
import 'package:ycbl/model/learn_task_list_entity.dart';
import 'package:ycbl/model/login_info_entity.dart';
import 'package:ycbl/model/my_course_model.dart';
import 'package:ycbl/model/params/params_model.dart';
import 'package:ycbl/model/user.dart';
import 'package:ycbl/net/http/bl_request_manager.dart';
import 'package:ycbl/ext/common_ext.dart';
import 'package:ycbl/utils/user_config.dart';

/// 网络请求仓库
class BLRepository {

  /// 登录
  static login(String phone, String code, Function(User) onSuccess, Function(BLErrorEntity) onError) {

    var loginParams = BLParamsLogin(phone, code: code).params;
    BLRequestManager.getInstance().post(
        BLConstant.api_login,
        queryParameters: loginParams,
        onSuccess: (json) {
          var loginInfoEntity = LoginInfoEntity.fromJson(json);
          var user = loginInfoEntity.data;
          if (user != null && user.stuId != null) {
            onSuccess(user);
          }else {
            onError(BLErrorEntity(code: loginInfoEntity.ret, message: loginInfoEntity.msg));
          }
        },
        onError: onError,
    );
  }

  /// 获取我的课程 - 班课
  static getMyNormalCourse(int userId, Function(List<MyCourseEntity> myCourseList) onSuccess, Function(BLErrorEntity err) onError) {
    var params = BLParams(
      tableId: 436,
      pageId: 5553,
      source: 1,
      limit: 100, /// 一般学生应该没有 100 个课程，再多 就不管了
    ).params;
    params['STU_ID_1_andOr'] = 0;
    params['STU_ID_numCondTwo_pld'] = 2;
    params['STU_ID_numValTwo_pld'] = null;
    params['STU_ID_numCondOne_pld'] = 2;
    params['STU_ID_numValOne_pld'] = userId;

    _getMyCourseList(MyCourseType.CLAZZ, params, onSuccess, onError);
  }

  /// 获取我的课程 - 一对一
  static getMyOneCourse(int userId, Function(List<MyCourseEntity> myCourseList) onSuccess, Function(BLErrorEntity err) onError) {
    var params = BLParams(
      tableId: 569,
      pageId: 3918,
      source: 1,
      limit: 100, /// 一般学生应该没有 100 个课程，再多 就不管了
    ).params;
    params['STU_ID_4_dicCond_pld'] = 0;
    params['STU_ID_4_andOr'] = 0;
    params['STU_ID_searchEleId'] = userId;
    params['STU_ID_searchEle'] = userId;
    params['PAY_STATUS_p_dicCond_pld'] = 0;
    params['PAY_STATUS_p_andOr'] = 0;
    params['PAY_STATUSstrCond_dic'] = 1;

    _getMyCourseList(MyCourseType.ONE, params, onSuccess, onError);
  }

  static _getMyCourseList(MyCourseType courseType, Map<String, dynamic> queryParameters, Function(List<MyCourseEntity> myCourseList) onSuccess, Function(BLErrorEntity) onError) {
    BLRequestManager.getInstance().get(
      BLConstant.api_list,
      queryParameters: queryParameters,
      onSuccess: (json) {
        var myCourseListRows = MyCourseListEntity.fromJson(json, courseType);
        if(myCourseListRows?.data?.rows != null) {
          onSuccess(myCourseListRows.data.rows);
        }else {
          onError(BLErrorEntity(code: myCourseListRows?.ret, message: myCourseListRows?.msg));
        }
      },
      onError: onError,
    );
  }

  /// 获取班课日历任务
  static getNormalCalendarTasks(DateTime startDate, DateTime endDate, int userId,
      Function(List<CalendarTaskEntity> calendarTaskList) onSuccess, Function(BLErrorEntity err) onError)
  {
    var params = BLParams(
      tableId: 422,
      pageId: 5855,
      source: 1,
      limit: 100,
    ).params;
    params['CLASS_START_TIME_3_andOr'] = 0;
    params['CLASS_START_TIME_dateType'] = 'yyyy-MM-dd';
    params['CLASS_START_TIME_startDates_pld'] = startDate.commonFormat();
    params['CLASS_START_TIME_endDates_pld'] = endDate.commonFormat();
    params['NEW_STU_ID_5_dicCond_pld'] = 0;
    params['NEW_STU_ID_5_andOr'] = 0;
    params['NEW_STU_ID_searchEleId'] = userId;
    params['NEW_STU_ID_searchEle'] = userId;

    _getCalendarTasks(params, onSuccess, onError);
  }

  /// 获取一对一日历任务
  static getClassCalendarTasks(DateTime startDate, DateTime endDate, int userId,
      Function(List<CalendarTaskEntity> calendarTaskList) onSuccess, Function(BLErrorEntity err) onError)
  {
    var params = BLParams(
      tableId: 586,
      pageId: 3920,
      source: 1,
      limit: 100,
    ).params;
    params['START_DATE_3_andOr'] = 0;
    params['START_DATE_dateType'] = 'yyyy-MM-dd';
    params['START_DATE_startDates_pld'] = startDate.commonFormat();
    params['START_DATE_endDates_pld'] = endDate.commonFormat();
    params['STU_ID_4_dicCond_pld'] = 0;
    params['STU_ID_4_andOr'] = 0;
    params['STU_ID_searchEleId'] = userId;
    params['STU_ID_searchEle'] = userId;

    _getCalendarTasks(params, onSuccess, onError);

  }

  static _getCalendarTasks(Map<String, dynamic> queryParameters, Function(List<CalendarTaskEntity> calendarTaskList) onSuccess, Function(BLErrorEntity) onError) {
    BLRequestManager.getInstance().get(
      BLConstant.api_list,
      queryParameters: queryParameters,
      onSuccess: (json) {
        var calendarListRows = CalendarTaskListEntity.fromJson(json);
        if(calendarListRows?.data?.rows != null) {
          onSuccess(calendarListRows.data.rows);
        }else {
          onError(BLErrorEntity(code: calendarListRows?.ret, message: calendarListRows?.msg));
        }
      },
      onError: onError,
    );
  }

  /// 获取 学习 - 班课 - 课程详情
  static getLearnClassDetail(int mainId, Function(LearnCourseDetailEntity courseDetail) onSuccess, Function(BLErrorEntity error) onError) {
    // http://ycsc.bailiedu.cn/portal/model_ajaxList.do?
    // source=1&CLASSID_4_dicCond_pld=0&CLASSID_4_andOr=0&tableId=436&pageId=2921
    // &STUID_4_dicCond_pld=0&STUID_4_andOr=0&STUID_searchEleId=10057670
    // &STUID_searchEle=10057670&CLASSID_searchEleId=714&CLASSID_searchEle=714

    var params = BLParams(
      source: 1,
      tableId: 436,
      pageId: 2921,
    ).params;

    int userId = UserConfig().getUserId();

    params['CLASSID_4_dicCond_pld'] = 0;
    params['CLASSID_4_andOr'] = 0;
    params['STUID_4_dicCond_pld'] = 0;
    params['STUID_4_andOr'] = 0;
    params['STUID_searchEleId'] = userId;
    params['STUID_searchEleId'] = userId;
    params['CLASSID_searchEleId'] = mainId;
    params['CLASSID_searchEle'] = mainId;

    _getLearnCourseDetail(params, onSuccess, onError);
  }

  /// 获取 学习 - 一对一 - 课程详情
  static getLearnOneDetail(int mainId,  Function(LearnCourseDetailEntity courseDetail) onSuccess, Function(BLErrorEntity error) onError) {
    // http://api.bailiedu.cn/?s=Ycsc.Com.Lists&source=1&tableId=569&pageId=3918
    // &start=0&limit=20_flow&subId_4_dicCond_pld=0&subId_4_andOr=0
    // &subId_searchEleId=237&subId_searchEle=237

    var params = BLParams(
      source: 1,
      tableId: 569,
      pageId: 3918,
    ).params;

    params['subId_4_dicCond_pld'] = 0;
    params['subId_4_andOr'] = 0;
    params['subId_searchEleId'] = mainId;
    params['subId_searchEle'] = mainId;

    _getLearnCourseDetail(params, onSuccess, onError);

  }

  static _getLearnCourseDetail(Map<String, dynamic> queryParameters, Function(LearnCourseDetailEntity courseDetail) onSuccess, Function(BLErrorEntity error) onError) {
    BLRequestManager.getInstance().get(
      BLConstant.api_list,
      queryParameters: queryParameters,
      onSuccess: (json) {
        var courseDetailRows = LearnCourseDetailRows.fromJson(json);
        if(courseDetailRows?.data?.rows != null && courseDetailRows.data.rows.isNotEmpty) {
          onSuccess(courseDetailRows.data.rows[0]);
        }else {
          onError(BLErrorEntity(code: courseDetailRows?.ret, message: courseDetailRows?.msg));
        }
      },
      onError: onError,
    );
  }

  /// 获取学习-班课-任务表
  static getLearnClassTaskList(int mainId, int start, int limit,
      Function(List<LearnTaskEntity>) onSuccess, Function(BLErrorEntity error) onError) {
    // http://api.bailiedu.cn/?s=Ycsc.Com.Lists&source=1&tableId=422&pageId=3458
    // &class_id_4_dicCond_pld=0&class_id_4_andOr=0&class_id_searchEleId=714
    // &class_id_searchEle=714&limit=1

    var params = BLParams(
      source: 1,
      tableId: 422,
      pageId: 3458,
      start: start,
      limit: limit,
    ).params;

    params['class_id_4_dicCond_pld'] = 0;
    params['class_id_4_andOr'] = 0;
    params['class_id_searchEleId'] = mainId;
    params['class_id_searchEle'] = mainId;

    _getLearnTaskList(params, onSuccess, onError);
  }

  /// 获取学习-一对一 -任务表
  static getLearnOneTaskList(int mainId, int start, int limit,
      Function(List<LearnTaskEntity>) onSuccess, Function(BLErrorEntity error) onError) {
    // http://api.bailiedu.cn/?s=Ycsc.Com.Lists&source=1&tableId=586&pageId=3920
    // &start=0&limit=20_flow&STU_ID_4_dicCond_pld=0&STU_ID_4_andOr=0
    // &STU_ID_searchEleId=10057670&STU_ID_searchEle=10057670
    // &STU_KEMU_ID_4_dicCond_pld=0&STU_KEMU_ID_4_andOr=0
    // &STU_KEMU_ID_searchEleId=208&STU_KEMU_ID_searchEle=208

    var params = BLParams(
      source: 1,
      tableId: 586,
      pageId: 3920,
      start: start,
      limit: limit,
    ).params;

    int userId = UserConfig().getUserId();

    params['STU_ID_4_dicCond_pld'] = 0;
    params['STU_ID_4_andOr'] = 0;
    params['STU_ID_searchEleId'] = userId;
    params['STU_ID_searchEle'] = userId;
    params['STU_KEMU_ID_4_dicCond_pld'] = 0;
    params['STU_KEMU_ID_4_andOr'] = 0;
    params['STU_KEMU_ID_searchEleId'] = mainId;
    params['STU_KEMU_ID_searchEle'] = mainId;

    _getLearnTaskList(params, onSuccess, onError);
  }

  static _getLearnTaskList(Map<String, dynamic> queryParameters, Function(List<LearnTaskEntity>) onSuccess, Function(BLErrorEntity error) onError) {
    BLRequestManager.getInstance().get(
      BLConstant.api_list,
      queryParameters: queryParameters,
      onSuccess: (json) {
        var learnTaskListRows = LearnTaskListEntity.fromJson(json);
        if(learnTaskListRows?.data?.rows != null) {
          onSuccess(learnTaskListRows.data.rows);
        }else {
          onError(BLErrorEntity(code: learnTaskListRows?.ret, message: learnTaskListRows?.msg));
        }
      },
      onError: onError,
    );
  }

  ///每日精品直播
  static getDailyLives(DateTime startDate, DateTime endDate, Function(List<FreeDailyLiveEntity>) onSuccess, Function(BLErrorEntity error) onError) {
    //http://api.bailiedu.cn/?s=Ycsc.Com.portalLists&tableId=422&pageId=6709
    // &COURSE_DATE_3_andOr=0&COURSE_DATE_dateType=yyyy-MM-dd
    // &COURSE_DATE_startDates_pld=2020-07-13&COURSE_DATE_endDates_pld=2020-07-20
    // &start=0&limit=1_flow&source=1

    var params = BLParams(
      source: 1,
      tableId: 422,
      pageId: 6709,
      start: 0,
      limit: 100,
    ).params;

    params['COURSE_DATE_3_andOr'] = 0;
    params['COURSE_DATE_dateType'] = 'yyyy-MM-dd';
    params['COURSE_DATE_startDates_pld'] = startDate.commonFormat();
    params['COURSE_DATE_endDates_pld'] = endDate.commonFormat();

    BLRequestManager.getInstance().get(
      BLConstant.api_portal_list,
      queryParameters: params,
      onSuccess: (json) {
        var dailyLiveListRows = FreeDailyLiveListEntity.fromJson(json);
        if(dailyLiveListRows?.data?.rows != null) {
          onSuccess(dailyLiveListRows.data.rows);
        }else {
          onError(BLErrorEntity(code: dailyLiveListRows?.ret, message: dailyLiveListRows?.msg));
        }
      },
      onError: onError,
    );
  }

}